package com.mobileClientKernel.android.reactNative.bridge;

import android.app.Activity;
import android.content.Intent;

import com.facebook.react.bridge.ActivityEventListener;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.mobileClientKernel.android.commonLayer.JJLog;
import com.mobileClientKernel.android.commonLayer.JSONUtil;
import com.mobileClientKernel.android.reactNative.manager.JJRNManager;
import com.mobileClientKernel.android.serviceManager.network.JJRequestError;
import com.mobileClientKernel.android.serviceManager.service.IJJServiceCallBack;
import com.mobileClientKernel.android.serviceManager.servicefactory.JJServiceFactory;
import com.mobileClientKernel.android.reactNative.service.reactNativeService.model.JJReactNativeModel;
import com.mobileClientKernel.android.reactNative.service.reactNativeService.service.JJReactNativeService;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by gongjian on 2018/4/28.
 */

public class JJRNBridge extends ReactContextBaseJavaModule implements ActivityEventListener {

    public JJRNBridge(ReactApplicationContext context) {
        super(context);

        JJRNManager.getInstance().setReactContext(context);
    }

    @Override
    public String getName() {
        return "JJRNBridge";
    }

    @Override
    public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {
    }

    @Override
    public void onNewIntent(Intent intent) {
    }

    @ReactMethod
    public void request(String optionJsonSting, final Promise promise) {
        JSONObject options = JSONUtil.getJSONObject(optionJsonSting);
        getReactNativeService(options).requestRN(options, new IJJServiceCallBack() {
            @Override
            public void onNetworkRequestSuccess(Object model, JSONObject otherInfo) {
                JJReactNativeModel rnModel = (JJReactNativeModel)model;
                String object = rnModel.getRnDataContent();
                promise.resolve(object);
            }

            @Override
            public void onNetworkRequestFailure(JJRequestError error, JSONObject otherInfo) {
                promise.reject(error.getCode().toString(), error.getMessage());
            }
        });
    }

    @ReactMethod
    public void cache(String optionJsonSting, final Promise promise) {
        JSONObject options = JSONUtil.getJSONObject(optionJsonSting);
        String object = getReactNativeService(options).getCacheRN(options);
        if (null == object) {
            promise.resolve("");
        }else  {
            promise.resolve(object);
        }
    }

    @ReactMethod
    public void removeCache(String optionJsonSting) {
        JSONObject options = JSONUtil.getJSONObject(optionJsonSting);
        getReactNativeService(options).removeCacheRN(options);
    }

    @ReactMethod
    public void saveCache(String optionJsonSting, String objectJsonString) {
        JSONObject options = JSONUtil.getJSONObject(optionJsonSting);
        getReactNativeService(options).saveCacheRN(options, objectJsonString);
    }

    private JJReactNativeService getReactNativeService(JSONObject options) {
        String serviceName = null;
        try {
            serviceName = options.getString("serviceName");
        } catch (JSONException e) {
            e.printStackTrace();
        }

        if (null == serviceName) {
            serviceName = "com.mobileClientKernel.android.reactNative.service.reactNativeService.service.JJReactNativeService";
        }

        return (JJReactNativeService) JJServiceFactory.getInstance().getService(serviceName);
    }

    /**
     * @desc 打印LOG
     */
    @ReactMethod
    public void log(String message, String logLevel) {
        //"0": debug, "1": info , "2": warn , "3": error
        switch (logLevel) {
            case "0":
                JJLog._d("rnLog", message);
                break;
            case "1":
                JJLog._i("rnLog", message);
                break;
            case "2":
                JJLog._w("rnLog", message);
                break;
            case "3":
                JJLog._e("rnLog", message);
                break;
        }

    }
}
